<template>
    <div class="box">
        <h2>Son2 子组件</h2>
        从vuex中获取的值:<label>{{ count }}</label>
        <br />
        <button @click="handleSub(1)">值 - 1</button>
        <button @click="handleSub(5)">值 - 5</button>
        <button @click="handleSub(10)">值 - 10</button>
        <button @click="handleSubWithParam(15)">值 - 15</button>
        <button @click="handleSubWithParam(20)">值 - 20</button>
        <button @click="changeTitle">改标题</button>
        <button @click="changeCountAsync(888)">改数据为888</button>
        <hr>
        <div>mapGetters {{ filterList }}</div>
        <hr>
        <div>测试访问模块中的state - mapState</div>
        <div>{{ user.userInfo.name }}</div>
        <div>{{ setting.theme }}</div>
        <div>user模块的name{{ userInfo.name }}</div>
        <button @click="updateUser">更新个人信息</button>
        <button @click="setUserSecond({ name: 'ab', age: 25 })">一秒后更新个人信息</button>
        <div>user模块的数据{{ userInfo }}</div>
        <button @click="updateTheme">更新主题色</button>
        <div>setting模块的数据{{ theme }} -- {{ desc }}</div>

        <div>测试访问模块中的getters - mapGetters</div>
        <div>{{ UpperCaseName }}</div>
    </div>
</template>

<script>
import { mapState, mapMutations, mapActions, mapGetters } from 'vuex'

export default {
  name: 'Son2Com',
  computed: {
    // mapState 和 mapGetters 都是映射属性
    ...mapState(['count', 'user', 'setting']),
    ...mapState('user', ['userInfo']),
    ...mapState('setting', ['theme', 'desc']),
    ...mapGetters(['filterList']),
    ...mapGetters('user', ['UpperCaseName'])
  },
  methods: {
    handleSub (n) {
      this.$store.commit('handleSubWithParam', n)
    },
    // mapMutations 和 mapActions 都是映射方法
    ...mapMutations(['handleSubWithParam', 'changeTitle']),
    ...mapMutations('user', ['setUser']),
    ...mapMutations('setting', ['setTheme']),
    ...mapActions(['changeCountAsync']),
    ...mapActions('user', ['setUserSecond']),
    updateUser () {
      this.setUser({ name: 'yy', age: 19 })
    },
    updateTheme () {
      this.setTheme('yellow')
    }
  }
}
</script>

<style lang="css" scoped>
.box {
    border: 3px solid #ccc;
    width: 400px;
    padding: 10px;
    margin: 20px;
}

h2 {
    margin-top: 10px;
}
</style>
